//
//  NationRepairHistoryDetailView.m
//  ICrEAM
//
//  Created by ysw on 2016/11/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "NationRepairHistoryDetailView.h"
#import "ICMRepairMaintenanceModel.h"
#import "ICMRepairPartsModel.h"

@interface NationRepairHistoryDetailView ()

@property (nonatomic, strong) UIView *maintenancesView;

@property (nonatomic, strong) UIView *partsView;

@property (nonatomic, strong) NSMutableArray *model;

@property (nonatomic, strong) NSMutableArray *partsModel;
@end

@implementation NationRepairHistoryDetailView

- (instancetype)initWithModel:(ICMRepairOrderModel *)model andFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.model = model.TT_RO_LABOUR;
        self.partsModel = model.TT_RO_REPAIR_PART;
        self.backgroundColor = TextFeildBackgroundColor_OffWhite;
        NSDictionary *titleTextDic = @{@"font":TitleFont,@"color":TitleColor_Dark,@"align":@1};
        
        //创建头部
        UIView *firstTitleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ActualHeight(46))];
        [self addSubview:firstTitleView];
        
        UILabel *firstTitleLabel = [self createLabelWithDic:titleTextDic text:@"维修项目" frame:CGRectMake(ActualWidth(50), 0, ActualWidth(100),ActualHeight(46))];
        [firstTitleView addSubview:firstTitleLabel];
        
        UIView *firstSep = [[UIView alloc] initWithFrame:CGRectMake(0,ActualHeight(46)-1, ScreenWidth, 1)];
        firstSep.backgroundColor = SepColor_gray;
        [firstTitleView addSubview:firstSep];
        
        //创建项目view
        int firstRowNum = ceilf(self.model.count/4.0);
        UIView *maintenancesView = [[UIView alloc]initWithFrame:CGRectMake(0, firstTitleView.bottomEdge, ScreenWidth, firstRowNum*ActualHeight(108))];
        maintenancesView.backgroundColor = [UIColor clearColor];
        self.maintenancesView = maintenancesView;
        //添加itemview
        for (int i = 0; i < self.model.count; i++) {
            ICMRepairMaintenanceModel *itemModel = self.model[i];
            UIView *itemView = [self createItemViewWithName:itemModel.LABOUR_NAME code:itemModel.LABOUR_CODE atIndex:i];
            [maintenancesView addSubview:itemView];
            BOOL isLastRow = (firstRowNum == ceilf((i+1)/4.0));
            if (!isLastRow) {
                UIView *horSep = [[UIView alloc]initWithFrame:CGRectMake(0, ceilf((i+1)/4.0)*ActualHeight(108)-1, ScreenWidth, 1)];
                horSep.backgroundColor = SepColor_gray;
                [maintenancesView addSubview:horSep];
            }
        }
        [self addSubview:maintenancesView];
        if (self.partsModel.count>0) {
            //创建头部
            UIView *secondTitleView = [[UIView alloc]initWithFrame:CGRectMake(0, maintenancesView.bottomEdge, ScreenWidth, ActualHeight(46))];
            [self addSubview:secondTitleView];
            
            UILabel *secondTitleLabel = [self createLabelWithDic:titleTextDic text:@"维修零件" frame:CGRectMake(ActualWidth(50), 0, ActualWidth(100),ActualHeight(46))];
            [secondTitleView addSubview:secondTitleLabel];
            
            UIView *secondSep = [[UIView alloc] initWithFrame:CGRectMake(0,ActualHeight(46)-1, ScreenWidth, 1)];
            secondSep.backgroundColor = SepColor_gray;
            [secondTitleView addSubview:secondSep];
            
            //创建零件view
            int rowNum = ceilf(self.partsModel.count/4.0);
            UIView *partsView = [[UIView alloc]initWithFrame:CGRectMake(0, secondTitleView.bottomEdge, ScreenWidth, rowNum*ActualHeight(108))];
            partsView.backgroundColor = [UIColor clearColor];
            self.partsView = partsView;
            //添加itemview
            for (int i = 0; i < self.partsModel.count; i++) {
                ICMRepairPartsModel *itemModel = self.partsModel[i];
                UIView *itemView = [self createItemViewWithName:itemModel.PART_NAME code:itemModel.PART_NO atIndex:i];
                [partsView addSubview:itemView];
                BOOL isLastRow = (firstRowNum == ceilf((i+1)/4.0));
                if (!isLastRow) {
                    UIView *horSep = [[UIView alloc]initWithFrame:CGRectMake(0, ceilf((i+1)/4.0)*ActualHeight(108)-1, ScreenWidth, 1)];
                    horSep.backgroundColor = SepColor_gray;
                    [partsView addSubview:horSep];
                }
            }
            [self addSubview:partsView];
        }
    }
    return self;
}

+ (instancetype)createNationRepairHistoryDetailViewWithModel:(ICMRepairOrderModel *)model andFrame:(CGRect)frame{
    return [[NationRepairHistoryDetailView alloc]initWithModel:model andFrame:frame];
}

- (UILabel *)createLabelWithDic:(NSDictionary *)dic text:(NSString *)text frame:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = dic[@"font"];
    label.textColor = dic[@"color"];
    label.textAlignment = [dic[@"align"] integerValue];
    return label;
}

- (UIView *)createItemViewWithName:(NSString *)name code:(NSString *)code atIndex:(int)index{
    CGFloat width = ScreenWidth/4.0;
    CGFloat height = ActualHeight(108);
    int row = index/4;
    int col = index%4;
    NSDictionary *nameTextDic = @{@"font":TitleFont,@"color":ContentTextColor_Black,@"align":@1};
    NSDictionary *codeTextDic = @{@"font":ContentFont,@"color":ContentTextColor_Gray,@"align":@1};
    UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake(col*width, row*height, width, height)];
    UILabel *nameLabel = [self createLabelWithDic:nameTextDic text:name frame:CGRectMake(ActualWidth(30), 0, width-ActualWidth(60), height/2)];
    nameLabel.numberOfLines = 0;
    [nameLabel sizeToFit];
    nameLabel.center = CGPointMake(width/2, height/2-nameLabel.height/2);
    
    UILabel *codeLabel = [self createLabelWithDic:codeTextDic text:code frame:CGRectMake(ActualWidth(30), nameLabel.bottomEdge+ActualHeight(10), width-ActualWidth(60), height/2-ActualHeight(10))];
    codeLabel.numberOfLines = 0;
    [codeLabel sizeToFit];
    codeLabel.center = CGPointMake(width/2, height/2+ActualHeight(10)+codeLabel.height/2);
    if ((index+1)%4 != 0) {
        UIView *verSep = [[UIView alloc]initWithFrame:CGRectMake(width-1, 0, 1, height)];
        verSep.backgroundColor = BackgroundColor_White;
        [itemView addSubview:verSep];
    }
    UIView *horSep = [[UIView alloc]initWithFrame:CGRectMake(0, height-1, width, 1)];
    horSep.backgroundColor = BackgroundColor_White;
    [itemView addSubview:horSep];
    
    [itemView addSubview:nameLabel];
    [itemView addSubview:codeLabel];
    
    return itemView;

}

@end
